/**
 * <p>
 * Title: 字符串处理的支持类
 * </p>
 * <p>
 * Description: 只能使用它的静态方法
 * </p>
 * <p>
 * Copyright: Copyright (c) 2011
 * </p>
 * <p>
 * </p>
 * 
 * @author 陈捷 赵明才
 * @version 1.0
 */
package atom.core0.util;

import org.apache.log4j.Logger;

public class DebugTime
{
	private long	         firstTime;
	private long	         startTime;
	private long	         endTime;
	private static long	         totalTime=0;
	private static long	         totalCount=0;
	private static double	         totalNumber=0;
	private static DebugTime	instance;
	private static boolean	 ifUserlog	= true;
	private static Logger	  log	   = Logger.getLogger(DebugTime.class);

	synchronized public static DebugTime getInstance()
	{
		if (instance == null)
		{
			instance = new DebugTime();
		}
		return instance;
	}

	public static void setIfUserlog(boolean ifUserlog)
	{
		DebugTime.ifUserlog = ifUserlog;
	}

	public DebugTime() {
		this.startTime = System.currentTimeMillis();
		this.firstTime = System.currentTimeMillis();
	}

	public void start()
	{
		this.startTime = System.currentTimeMillis();
	}

	public long end()
	{
		this.endTime = System.currentTimeMillis();
		long tt = this.endTime - this.startTime;
		if (ifUserlog)
			log.debug("【" + (this.endTime - this.startTime) + "】毫秒.");
		else
			System.out.println("【" + (this.endTime - this.startTime) + "】毫秒.");
		this.startTime = System.currentTimeMillis();
		return tt;
	}

	/**
	 * 设置结束时间，并且打印日志
	 * 
	 * @param logPrefix
	 */
	public long end(String logPrefix)
	{
		this.endTime = System.currentTimeMillis();
		long tt = this.endTime - this.startTime;
		if (ifUserlog)
			log.debug(logPrefix + "【" + (this.endTime - this.startTime) + "】毫秒.");
		else
			System.out.println(logPrefix + "【" + (this.endTime - this.startTime) + "】毫秒.");
		this.startTime = System.currentTimeMillis();
		return tt;
	}
	public long getEndtime()
	{
		long tt = System.currentTimeMillis() - this.startTime;
		return tt;
	}
	/**
	 * 设置结束时间,并返回总耗时时间
	 * 
	 * @param logPrefix
	 * @return
	 */
	public void endtotal(String logPrefix)
	{
		this.endTime = System.currentTimeMillis();
		if (ifUserlog)
			log.debug(logPrefix + "【" + (this.endTime - this.firstTime) + "】毫秒.");
		else
			System.out.println(logPrefix + "【" + (this.endTime - this.firstTime) + "】毫秒.");
		this.startTime = System.currentTimeMillis();
	}

	public static long getTotalTime()
	{
		return totalTime;
	}

	public static void setTotalTime(long tTime)
	{
		totalTime = tTime;
	}
	public static synchronized void increaseTotalTime(long tTime)
	{
		totalTime += tTime;
	}

	public static long getTotalCount()
    {
    	return totalCount;
    }

	public static void setTotalCount(long tCount)
    {
    	DebugTime.totalCount = totalCount;
    }

	public static double getTotalNumber()
    {
    	return totalNumber;
    }

	public static void setTotalNumber(double tNumber)
    {
    	DebugTime.totalNumber = totalNumber;
    }
	public static synchronized double increaseTotalNumber(double tNumber)
	{
		totalNumber += tNumber;
		return totalNumber;
	}
	public static synchronized long increaseTotalCount(long tCount)
	{
		totalCount += tCount;
		return totalCount;
	}	
}
